package org.huang.spock.spring

import org.huang.spock.spring.bo.Dept
import org.huang.spock.spring.bo.Person
import org.huang.spock.spring.dao.DeptAnnoMapper
import org.huang.spock.spring.service.DemoService
import org.junit.runner.RunWith
import org.spockframework.spring.SpringBean
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner
import spock.lang.Specification

import java.util.stream.Collectors


@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE)
//@ActiveProfiles("test")
class SpringBeanMock2Test extends Specification {

    @Autowired
    private DemoService demoService;

    //Mock bean： deptAnnoMapper
    @SpringBean
    private DeptAnnoMapper deptAnnoMapper = Mock()

    //spring 集成接口测试
    def "test getByDeptId"() {
        given:
        deptAnnoMapper.getById(_) >> new Dept(id: 1, name: 'xx', code: 'qq')
        when:
        def result = demoService.getByDeptId(id)
        then:
        with(result) {
            dept == expectDept
            persons.stream().map(Person::getName).collect(Collectors.toList()) == expectPersonNames
        }
        where:
        id || expectDept                              | expectPersonNames
        1  || new Dept(id: 1, name: 'xx', code: 'qq') | ['张三', '里斯', '王五']
    }

}